package com.soxing.braindata.service.manager;



import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.collect.Lists;
import com.soxing.braindata.dao.domain.request.SearchRequest;
import com.soxing.braindata.dao.entity.Site;
import com.soxing.braindata.dao.entity.StatItemResult;
import com.soxing.braindata.dao.service.ISiteService;
import com.soxing.braindata.dao.service.IStatItemResultService;
import com.soxing.braindata.domain.enumeration.ComplianceRateEnum;
import com.soxing.braindata.domain.enumeration.QualityRateEnum;
import com.soxing.braindata.domain.enumeration.SexRateEnum;
import com.soxing.braindata.domain.request.SiteSearchRequest;
import com.soxing.braindata.domain.response.RStat;
import com.soxing.braindata.domain.response.SStat;
import com.soxing.braindata.domain.stat.CollectEntity;
import com.soxing.braindata.domain.stat.FinishEntity;
import com.soxing.braindata.domain.stat.QAEntity;
import com.soxing.braindata.domain.stat.YearEntity;
import com.soxing.common.utils.response.WebResponse;
import com.soxing.common.utils.util.ObjectMapper;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author :durango
 * @date :2022/6/11 15:03
 * @description 中心管理服务实现
 */
@Service
public class SiteManagerImpl extends AbstractManager<Site> implements ISiteManager {

    @Resource
    private ISiteService siteService;

    @Override
    public IService<Site> getService() {
        return siteService;
    }
    @Resource
    private IStatItemResultService statItemResultService;

    @Override
    public LambdaQueryWrapper<Site> wrapSearchRequest(SearchRequest request) {
        SiteSearchRequest searchRequest = (SiteSearchRequest) request;
        if (request == null) {
            searchRequest = new SiteSearchRequest();
        }
        Site obj = ObjectMapper.map(searchRequest, Site.class);
        LambdaQueryWrapper<Site> queryWrapper = new LambdaQueryWrapper<>(obj);
        if (StringUtils.isNotBlank(searchRequest.getSiteName())) {
            obj.setSiteName(null);
            queryWrapper.like(Site::getSiteName, searchRequest.getSiteName());
        }
        return queryWrapper;
    }


    @Override
    public List<Site> getSiteBysitePkIds(Set<String> sitePkIdSet) {
        if (CollectionUtils.isEmpty(sitePkIdSet)) {
            return Lists.newArrayList();
        }
        List<Site> siteList = siteService.list(new LambdaQueryWrapper<Site>().in(Site::getSitePkId, sitePkIdSet));
        Map<String, Site> siteMap = siteList.stream().collect(Collectors.toMap(Site::getSitePkId, Function.identity(), (a, b) -> a));
        return Lists.newArrayList(siteMap.values());
    }

    @Override
    public Site getSiteBysitePkId(String sitePkId) {
        if (StringUtils.isBlank(sitePkId)) {
            return null;
        }
        List<Site> siteList = siteService.list(new LambdaQueryWrapper<Site>().eq(Site::getSitePkId, sitePkId));
        if (CollectionUtils.isEmpty(siteList)) {
            return null;
        }
        return siteList.get(0);
    }

    @Override
    public WebResponse<SStat> getSiteStat(String subjectPkId,String sitePkId)
    {
        SStat sstat = new SStat();
//        List<StatItemResult> man = statItemResultService.list(new LambdaQueryWrapper<StatItemResult>().in(StatItemResult::getSitePkId,sitePkId).in(StatItemResult::getItemCode, SexRateEnum.MAN.getKey()));
//        statItemResultService.list(new LambdaQueryWrapper<StatItemResult>().in(StatItemResult::getSitePkId,sitePkId).in(StatItemResult::getItemCode, SexRateEnum.WOMAN.getKey()));
        List<StatItemResult> statItemList = statItemResultService.list(new LambdaQueryWrapper<StatItemResult>().in(StatItemResult::getSitePkId,sitePkId).in(StatItemResult::getSubjectPkId,subjectPkId));
        statItemList.forEach(
                item->{
                    if(item.getItemCode().equals(SexRateEnum.WOMAN.getKey()))
                    {
                        sstat.getSexRate().put(SexRateEnum.WOMAN.getName(),item.getStatResult().longValue());
                    }
                    else if(item.getItemCode().equals(SexRateEnum.MAN.getKey()))
                    {
                        sstat.getSexRate().put(SexRateEnum.MAN.getName(),item.getStatResult().longValue());
                    }


                    else if(item.getItemCode().equals(QualityRateEnum.HIGH.getKey()))
                    {
                        sstat.getQualityRate().put(QualityRateEnum.HIGH.getName(),item.getStatResult().longValue());

                    }
                    else if(item.getItemCode().equals(QualityRateEnum.MIDDLE.getKey()))
                    {
                        sstat.getQualityRate().put(QualityRateEnum.MIDDLE.getName(),item.getStatResult().longValue());

                    }
                    else if(item.getItemCode().equals(QualityRateEnum.LOW.getKey()))
                    {
                        sstat.getQualityRate().put(QualityRateEnum.LOW.getName(),item.getStatResult().longValue());

                    }


                    else if(item.getItemCode().equals(ComplianceRateEnum.HIGH.getKey()))
                    {
                        sstat.getComplianceRate().put(ComplianceRateEnum.HIGH.getName(),item.getStatResult().longValue());
                    }
                    else if(item.getItemCode().equals(ComplianceRateEnum.MIDDLE.getKey()))
                    {
                        sstat.getComplianceRate().put(ComplianceRateEnum.MIDDLE.getName(),item.getStatResult().longValue());
                    }
                    else if(item.getItemCode().equals(ComplianceRateEnum.LOW.getKey()))
                    {
                        sstat.getComplianceRate().put(ComplianceRateEnum.LOW.getName(),item.getStatResult().longValue());
                    }

                    else if(item.getItemCode().equals(CollectEntity.key))
                    {

                        HashMap<String,Object> map =
                                JSONObject.parseObject(item.getStatJson(), HashMap.class);


                        Map<String,Object> treestat = new TreeMap<>(new Comparator<Object>() {

                            @SneakyThrows
                            @Override
                            public int compare(Object o1, Object o2) {
                                if(o1 instanceof String && o2 instanceof String) {
                                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                    Date sd1=df.parse((String) o1);
                                    Date sd2=df.parse((String) o2);
                                    return sd1.compareTo(sd2);
                                }
                                return 0;
                            }

                        });
                        treestat.putAll(map);
                        sstat.setCollectionBar(treestat);
                    }
                    else if(item.getItemCode().equals(FinishEntity.key))
                    {
                        HashMap<String,Object> map =
                                JSONObject.parseObject(item.getStatJson(), HashMap.class);
                        sstat.setFinishBar(map);

                    }
                    else if(item.getItemCode().equals(QAEntity.key))
                    {
                        HashMap<String,Object> map =
                                JSONObject.parseObject(item.getStatJson(), HashMap.class);
                        sstat.setQABar(map);
                    }
                    else if(item.getItemCode().equals(YearEntity.key))
                    {
                        HashMap<String,Object> map =
                                JSONObject.parseObject(item.getStatJson(), HashMap.class);
                        sstat.setYearBar(map);
                    }





                }
        );
        return WebResponse.success(sstat);


    }










}

